CaptureBuildings = {}
CaptureBuildings.__index = CaptureBuildings
 
function CaptureBuildings.new()
    local instance = {

    }
    
    setmetatable(instance, CaptureBuildings)
    return instance
end

function CaptureBuildings:capture(unit, buildingUniton, buildingProp)
  local health = unit:getHealth()
  buildingUniton:increaseCurrentCapturePoints(health)
  
  if buildingUniton:getCurrentCapturePoints() >= buildingUniton:getCapturePoints() then
     buildingUniton:setOwnerObject(unit:getOwnerObject())
     buildingUniton:setOwner(unit:getOwnerObject():getOwnerName())
     buildingUniton:resetCurrentCapturePoints()
     unit:getOwnerObject():increaseGold(350)
    local typeBuilding = buildingUniton:getType()
    
    if unit:getOwnerObject() == pl1 then
      pl2:removeBuildingFromList(buildingUniton)
      pl1:addBuildingToList(buildingUniton)
      
      if typeBuilding == "BarracksCavalry-neutral" then
        buildingUniton:setType("BarracksCavalry-player1")
      elseif typeBuilding == "BarracksMelee-neutral" then
        buildingUniton:setType("BarracksMelee-player1")
      elseif typeBuilding == "BarracksRange-neutral" then
        buildingUniton:setType("BarracksRange-player1")
      end
    else
     pl1:removeBuildingFromList(buildingUniton)
     pl2:addBuildingToList(buildingUniton)

     if typeBuilding == "BarracksCavalry-neutral" then
       buildingUniton:setType("BarracksCavalry-player2")
     elseif typeBuilding == "BarracksMelee-neutral" then
       buildingUniton:setType("BarracksMelee-player2")
     elseif typeBuilding == "BarracksRange-neutral" then
       buildingUniton:setType("BarracksRange-player2")
     end
    end
    
    if typeBuilding == "CapturePointBuilding-neutral" then
      self:captureHeroBuilding(unit, buildingUniton, buildingProp)
    elseif typeBuilding == "CapturePointBuilding-player1" then
      self:captureHeroBuilding(unit, buildingUniton, buildingProp)
    elseif typeBuilding == "CapturePointBuilding-player2" then
      self:captureHeroBuilding(unit, buildingUniton, buildingProp)
    end
    
    local capturedBuilding = MOAITextBox.new()
    capturedBuilding:setStyle(style)
    capturedBuilding:setRect(130, 0, -380, -100)
    capturedBuilding:setAlignment(MOAITextBox.CENTER_JUSTIFY, MOAITextBox.CENTER_JUSTIFY)
    capturedBuilding:setYFlip(true)
    capturedBuilding:setPriority(50)
   
    capturedBuilding:setString("You have captured a building")
    renderLayerTable[5] = capturedBuilding
    MainLayer:insertProp(capturedBuilding)
    
    local timer = MOAITimer.new()
    timer:setMode(MOAITimer.NORMAL)
    timer:setSpan(2)
    timer:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function()
        renderLayerTable[5] = nil
        MainLayer:removeProp(capturedBuilding)
        end)
    timer:start()
    
    local victory = MOAIGfxQuad2D.new()
    victory:setTexture("resources/graphics/menu/victory_screen.png")
    victory:setRect(-384, -370, 384, 320)

    victoryProp = MOAIProp2D.new()
    victoryProp:setDeck(victory)
    victoryProp:setLoc(384, -320)
    victoryProp:setPriority(1337)
    victoryProp:setBlendMode(MOAIProp.GL_SRC_ALPHA)
    
    winMessage = MOAITextBox.new()
    winMessage:setStyle(style)
    winMessage:setRect(200, 50, -200, -100)
    winMessage:setAlignment(MOAITextBox.CENTER_JUSTIFY, MOAITextBox.CENTER_JUSTIFY)
    winMessage:setYFlip(true)
    renderLayerTable[5] = winMessage

    local spriteBuilding = MOAIGfxQuad2D.new()
    spriteBuilding:setRect(buildingProp:getRect())
    setTexturePlayer(spriteBuilding, buildingUniton)

    local propBuilding = MOAIProp2D.new()
    propBuilding:setPriority(20)
    propBuilding:setDeck(spriteBuilding)
    propBuilding:setBlendMode(MOAIProp.GL_SRC_ALPHA, MOAIProp.GL_ONE_MINUS_SRC_ALPHA)

    MainLayer:removeProp(buildingProp)
    propToTileObject[buildingProp] = nil

    propToTileObject[propBuilding] = buildingUniton
    MainLayer:insertProp(propBuilding)
    
    if buildingUniton:getType() == "Headquarters-player1" then
      showMainMenu()
      MainLayer:clear()
      MainLayer:setClearColor(0, 0, 1, 1)
      MainLayer:insertProp(victoryProp)
      winMessage:setString(pl2:getName() .. " won!")
      winMessage:setPriority(1338)
      MainLayer:insertProp(winMessage)
   
      isVictoryScreen = true

    elseif buildingUniton:getType() == "Headquarters-player2" then
      showMainMenu()
      MainLayer:clear()
      MainLayer:setClearColor(1, 0, 0, 1)
      MainLayer:insertProp(victoryProp)
      winMessage:setString(pl1:getName() .. " won!")
      winMessage:setPriority(1338)
      MainLayer:insertProp(winMessage)

      isVictoryScreen = true
    end
    
    return true
  else
    return false
  end
end

function CaptureBuildings:captureHeroBuilding(unit, buildingUniton, buildingProp)
  if unit:getOwnerObject() == pl1 then
    buildingUniton:setType("CapturePointBuilding-player2")
    pl2:removeBuildingFromList(buildingUniton)
    pl1:addBuildingToList(buildingUniton)
  else
    buildingUniton:setType("CapturePointBuilding-player1")
    pl1:removeBuildingFromList(buildingUniton)
    pl2:addBuildingToList(buildingUniton)
  end
      
  if unit:getType() == "SwordFighter" then
    unit:increaseHealthBy(10)
    unit:setDamageMultiplier(1.5)
  elseif unit:getType() == "Heavy_Horse" then
    unit:setMoveRange(4)
    unit:setDamageMultiplier(1.5)
  elseif unit:getType() == "Archer" then
    unit:setAttackRange(3, true)
    unit:setDamageMultiplier(1.5)
  end
  
  unit:setIsHero(true)
      
  local unitProp = nil
  for kk, vv in pairs (propToTileObject) do
    if unit == vv then
      unitProp = kk
    end
  end
      
  MainLayer:removeProp(unitProp)
  
  local spriteHero = MOAIGfxQuad2D.new()
  spriteHero:setRect(unitProp:getRect())
  local x1, y1, x2, y2 = unitProp:getRect()
  
  if unit:getOwnerObject() == pl1 then
    if unit:getType() == "Archer" then
      spriteHero:setTexture(textureArcherRedHero)
    elseif unit:getType() == "Heavy_Horse" then
      spriteHero:setTexture(textureKnightRedHero)
    elseif unit:getType() == "SwordFighter" then
      spriteHero:setTexture(textureSwordRedHero)
    end
  else
    if unit:getType() == "Archer" then
      spriteHero:setRect(x2, y1, x1, y2)
      spriteHero:setTexture(textureArcherBlueHero)
    elseif unit:getType() == "Heavy_Horse" then
      spriteHero:setRect(x2, y1, x1, y2)
      spriteHero:setTexture(textureKnightBlueHero)
    elseif unit:getType() == "SwordFighter" then
      spriteHero:setRect(x2, y1, x1, y2)
      spriteHero:setTexture(textureSwordBlueHero)
    end
  end

  unitProp:setDeck(spriteHero)
  unitProp:setPriority(30)
  unitProp:setBlendMode(MOAIProp.GL_SRC_ALPHA)
  MainLayer:insertProp(unitProp)
  
  local unitConvertedHero = MOAITextBox.new()
  unitConvertedHero:setStyle(style)
  unitConvertedHero:setRect(175, -25, -250, -125)
  unitConvertedHero:setAlignment(MOAITextBox.CENTER_JUSTIFY, MOAITextBox.CENTER_JUSTIFY)
  unitConvertedHero:setYFlip(true)
  unitConvertedHero:setString("Your unit became a Hero!")
  renderLayerTable[6] = unitConvertedHero
  MainLayer:insertProp(unitConvertedHero)
  
  local timer = MOAITimer.new()
  timer:setMode(MOAITimer.NORMAL)
  timer:setSpan(2)
  timer:setListener(MOAITimer.EVENT_TIMER_END_SPAN, function()
    renderLayerTable[6] = nil
    MainLayer:removeProp(unitConvertedHero)
    end)
  timer:start()
end

function CaptureBuildings:removeUnitFromCaptureList(_unitInList)
  for k, v in pairs(captureBuildingsList) do
    local unit = v[1]
    local buildingObject = v[2]
    if unit == _unitInList then
      buildingObject:resetCurrentCapturePoints()
      buildingObject:removeCaptureTextbox()
      table.remove(captureBuildingsList, k)
    end
  end
end

return CaptureBuildings